package com.unicloud.medical.langchao.autoconfigure;

import com.amazonaws.ClientConfiguration;
import com.amazonaws.Protocol;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.S3ClientOptions;
import com.unicloud.medical.langchao.LangChaoService;
import com.unicloud.medical.langchao.starter.LangChaoHealthCheckStarter;
import com.unicloud.medical.oss.common.beanFactoryPostProcessor.AbstractBeanFactoryPostProcessor;
import com.unicloud.medical.oss.common.constant.OSSTypeConstant;
import com.unicloud.medical.oss.common.model.OSSServerProperties;
import com.unicloud.medical.oss.common.util.CommonUtil;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.ConfigurableApplicationContext;

public class LangChaoBeanFactoryPostProcessor extends AbstractBeanFactoryPostProcessor {
    public LangChaoBeanFactoryPostProcessor(ConfigurableApplicationContext applicationContext, String ossType) {
        super(applicationContext, ossType);
    }

    @Override
    protected String type() {
        return OSSTypeConstant.LANG_CHAO;
    }

    @Override
    protected void registerClient(OSSServerProperties ossServerProperties, BeanDefinitionRegistry registry) throws Exception {
        // S3client
        ClientConfiguration clientConfig = new ClientConfiguration();
        clientConfig.setSignerOverride("S3SignerType");
        clientConfig.setProtocol(Protocol.HTTP);

        AWSCredentials awsCredentials = new BasicAWSCredentials(ossServerProperties.getAccessKey(), ossServerProperties.getSecretKey());
        AmazonS3 s3Client = new AmazonS3Client(awsCredentials, clientConfig);
        s3Client.setEndpoint(ossServerProperties.getEndpoint());
        s3Client.setS3ClientOptions(S3ClientOptions.builder().setPathStyleAccess(true).build());

        String name = CommonUtil.buildName(ossServerProperties.getName(), "langchao", ossServerProperties.getEndpoint());

        // LangChaoService
        registerBeanDefinition(registry, name + "LangChaoService",
                BeanDefinitionBuilder
                        .genericBeanDefinition(LangChaoService.class)
                        .addPropertyValue("name", name)
                        .addPropertyValue("langChaoProperties", ossServerProperties)
                        .addPropertyValue("s3client", s3Client)
                        .getBeanDefinition());

        // LangChaoHealthCheckStarter
        registerBeanDefinition(registry, name + "LangChaoHealthCheckStarter",
                BeanDefinitionBuilder
                        .genericBeanDefinition(LangChaoHealthCheckStarter.class)
                        .addPropertyValue("s3client", s3Client)
                        .addPropertyValue("check", ossServerProperties.getCheck())
                        .getBeanDefinition());
    }

}
